package com.ghh.dao;

import com.ghh.pojo.Role;
import com.ghh.pojo.User;
import com.github.pagehelper.Page;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

public interface UserDao {
    @Select("select * from t_user where username=#{username}")
    User findByUsername(String username);

    //查询用户管理分页操作，并条件查询
    @Select("<script>select * from t_user <if test=\"value!=null and value!=''\">where username=#{value}</if></script>")
    Page<User> findPage(@Param("value")  String queryString);

    //查询所有角色
    @Select("select *from t_role")
    List<Role> findAll();

    //添加
    @Insert("insert into t_user values (null,#{birthday},#{gender},#{username},#{password},#{remark},#{station},#{telephone})")
    @SelectKey(statement = "select last_insert_id()",before = false,keyProperty = "id",resultType = Integer.class,keyColumn = "id")
    void add(User user);

    //添加用户与角色的关联表
    @Insert("insert into t_user_role (user_id,role_id) values (#{user_id},#{role_id})")
    void setUserAndRole(Map map);

    //根据用户ID 查询 关联的角色id
    @Select("select role_id from t_user_role where user_id = #{id}")
    List<Integer> findRoleIdsByUserId(Integer id);

    //根据用户ID查询用户数据
    @Select("select * from t_user where id = #{id}")
    User findById(Integer id);

    //编辑用户信息
    @Update("update t_user set birthday = #{birthday},gender = #{gender},username = #{username},password = #{password},remark = #{remark},station = #{station},telephone = #{telephone} where id = #{id}")
    void edit(User user);

    //删除用户iD与角色ID
    @Delete("delete from t_user_role where user_id = #{id}")
    void deleteUserAndRoleById(Integer id);

    //删除用户信息
    @Delete("delete from t_user where id = #{id}")
    void deleteUserById(Integer id);

}
